﻿using Elio.Controls;
using Elio.Interfaces;
using Elio.Sessions;
using Elio.Sessions.Widgets;
using Google.Protobuf.WellKnownTypes;

namespace Elio.Widgets;

public class WidgetText : Label, ISessionWidget
{
    public BaseSession? Session { get; set; }
    public void Init(InitOptions options)
    {
        (WidthRequest, HeightRequest) = (options.Width, options.Height);
        this.SetPoint(options.X, options.Y);
        Text = options.Text;
    }

    public async Task<bool> OnViewReceive(string name, long sid, long fid, params Any[] argv)
    {
        switch (name)
        {
            case "getText":
                await GetText(sid, fid);
                break;

            case "setText":
                SetText(argv);
                break;

            default:
                return false;
        }
        return true;
    }

    private async Task GetText(long sid, long fid) =>
        await Session!.SendAction("onGetText", sid, fid, Text.ToAny());

    private void SetText(params Any[] argv) => Text = argv[0].P2String();
}
